<?php
/**商品表模型
 * @Author: 976123967@qq.com
 * @Date:   2014-08-31 16:44:06
 * @Last Modified by:   chen
 * @Last Modified time: 2014-09-08 23:32:04
 */
class GoodsModel extends Model{
	// 指定表名
	public $table='goods';

	/**
	 * [$validate 自动验证]
	 * @var array
	 */
	public $validate = array(
		// 商铺
		array('shopid','regexp:/^[1-9]\d*$/','请选择商铺',2,3),
		// 商品分类
		array('cid','regexp:/^[1-9]\d*$/','请选择商品分类',2,3),
		// 地区
		array('lid','regexp:/^[1-9]\d*$/','请选择地区',2,3),
		// 主标题
		array('main_title','nonull','请输入主标题',2,3),
		// 副标题
		array('sub_title','nonull','请输入副标题',2,3),
		// 现价
		array('price','nonull','请输入现价',2,3),
		// 原价
		array('old_price','nonull','请输入原价',2,3),
		// 开始时间
		array('start_time','nonull','开始时间必须选择',2,3),
		// 结束时间
		array('end_time','nonull','结束时间必须选择',2,3),
	);

	/**
	 * [$auto 自动完成]
	 * @var array
	 */
	public $auto = array(
		array('start_time','strtotime','function',2,3),
		array('end_time','strtotime','function',2,3),
		array('buy','intval','function',2,3)
	);

	

	/**
	 * [alter_goods_img 图片字段自动完成]
	 * @return [type] [description]
	 */
	public function alter_goods_img()
	{
		
		if(isset($_POST['upload']) && $_POST['upload'])
		{
			// 编辑时候删除图片
			if( isset($_POST['gid']))
			{
				$gid = Q('post.gid');
				$this->del_goods_img($gid);
			}
			

			return $_POST['upload'][1]['path'];
		}
		return '';
	}

	/**
	 * [get_goods_all 所有商品]
	 * @param  [type] $where [description]
	 * @param  [type] $limit [description]
	 * @return [type]        [description]
	 */
	public function get_goods_all($where=null,$limit=null)
	{
		return $this->where($where)->limit($limit)->select();
	}



	/**
	 * [add_goods 添加]
	 */
	public function add_goods()
	{
		if($img = $this->alter_goods_img())
		{
			$this->data['goods_img']= $img;
		}
		return $this->add();
	}


	/**
	 * [edit_goods 编辑]
	 * @return [type] [description]
	 */
	public function edit_goods()
	{
		if($img = $this->alter_goods_img())
		{
			$this->data['goods_img']= $img;
		}
		$this->update();
	}




	/**
	 * [del_goods_img 商品图片删除]
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function  del_goods_img($gid)
	{
		$goodsImg = $this->where(array('gid'=>$gid))->getField('goods_img');
		if(!is_file($temp)) return false;
	
		$pic = pathinfo($goodsImg);
	
		$temp = $pic['dirname'].'/'.$pic['filename'].'_460x280'.'.'.$pic['extension'];
		is_file($temp) && unlink($temp);

		$temp = $pic['dirname'].'/'.$pic['filename'].'_200x100'.'.'.$pic['extension'];
		is_file($temp) && unlink($temp);

		$temp = $pic['dirname'].'/'.$pic['filename'].'_310x185'.'.'.$pic['extension'];
		is_file($temp) && unlink($temp);

		$temp = $pic['dirname'].'/'.$pic['filename'].'_90x55'.'.'.$pic['extension'];
		is_file($temp) && unlink($temp);

		is_file($goodsImg) && unlink($goodsImg);
	}



	/**
	 * [del_goods 商品删除]
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function del_goods($gid)
	{
		$this->del_goods_img($gid);
		$this->delete($gid);
	}


	/**
	 * [get_goods_one 单条信息]
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	public function get_goods_one($gid)
	{
		$field = 'main_title,goods_img,price,end_time,start_time,gid';
		$data = $this->field($field)->find($gid);
		if(!$data) 
			return $data;
		else
		{
			$data['status'] = time()>$data['start_time'] &&time()<$data['end_time']?'可购买':'已下架';
			// 图片
			$pic = pathinfo($data['goods_img']);
			$data['cart_img'] = __ROOT__.'/'.$pic['dirname'].'/'.$pic['filename'].'_90x55'.".".$pic['extension'];
		}
		return $data;
	}

}